package com.dmgis.qxfw.dataservice.businessservice.yydl.service.Impl;

import com.dmgis.qxfw.dataservice.businessservice.api.domain.DbConfig;
import com.dmgis.qxfw.dataservice.businessservice.api.domain.RequestDataBySelect;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.qx.all_today_forecast;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.qx.today_forecast;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.qx.township_forecast;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.qx.township_forecasttemp;

import com.dmgis.qxfw.dataservice.businessservice.common.util.JsonUtil;
import com.dmgis.qxfw.dataservice.businessservice.common.util.StringUtil;
import com.dmgis.qxfw.dataservice.businessservice.controller.ProcessRequestController;
import com.dmgis.qxfw.dataservice.businessservice.yydl.service.DayForecastServiceI;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class DayForecastServiceImpl implements DayForecastServiceI {

	ProcessRequestController pc = new ProcessRequestController();

	@Override
	public List<Map<String, Object>> getDayForecast(String dateTime) {
		List<township_forecasttemp> listData = new ArrayList<township_forecasttemp>();
		String timeChar = "";
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("max(timeChar) as timeChar");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		if (dateTime != null) {
			strWhere.append(String.format(" and datechar='%s'", dateTime));
		}
		strWhere.append(String.format(" and ntimes='%s'", "24"));
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		
		try {
			selectParam.put("method", "township_forecasttemp");
			selectParam.put("sqlType", "select");
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<township_forecasttemp>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (listData != null && listData.size() > 0) {
			timeChar = listData.get(0).getTimechar();
		}
		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		Map<String, String> selectParam1 = new HashMap<String, String>();
		RequestDataBySelect selectRequest1 = new RequestDataBySelect();
		List<String> columns1 = new ArrayList<String>();
		columns1.add("stationname");
		columns1.add("weather1");
		columns1.add("CAST(replace(cast(maxtemp AS VARCHAR),'-65535','99999') as numeric(18,1)) as maxtemp");
		columns1.add("CAST(replace(cast(mintemp AS VARCHAR),'-65535','99999') as numeric(18,1)) as mintemp");
		columns1.add("winds");
		columns1.add("windd");
		columns1.add("CAST(replace(cast(humid AS VARCHAR),'-65535','99999') as numeric(18,1)) as humid");
		columns1.add("longitude");
		columns1.add("latitude");
		selectRequest1.setColumns(columns1.toArray(new String[columns1.size()]));
		// 拼接where字段
		StringBuilder strWhere1 = new StringBuilder();
		int whereCount1 = 0;
		if (dateTime != null) {
			strWhere1.append(String.format(" and datechar='%s'", dateTime));
		}
		strWhere1.append(String.format(" and timechar='%s'", timeChar));
		strWhere1.append(String.format(" and ntimes<='%s'", "24"));
		strWhere1.append(String.format(" and stationid like '%s%%'", DbConfig.STATIONID));
		if (whereCount1 == 0 && !StringUtil.IsNullOrEmpty(strWhere1.toString())) {
			strWhere1 = strWhere1.replace(0, 4, "");
		}
		selectRequest1.setParam(strWhere1.toString());
		try {
			selectParam1.put("method", "township_forecasttemp");
			selectParam1.put("sqlType", "select");
			selectParam1.put("param", JsonUtil.object2Json(selectRequest1));
			String message1 = pc.processRequest(selectParam1);
			resultList = JsonUtil.jsonStr2TypeReference(message1, new TypeReference<List<Map<String, Object>>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
			System.err.println(e.getStackTrace());
		}
		return resultList;
		
	}
	@Override
	public List<Map<String, Object>> getDayForecast1(String dateTime) {
		List<township_forecast> listData = new ArrayList<township_forecast>();
		String result = "";
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("max(timeChar) as timeChar");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		if (dateTime != null) {
			strWhere.append(String.format(" and datechar='%s'", dateTime));
		}
		strWhere.append(String.format(" and ntimes='%s'", "24"));
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		
		try {
			selectParam.put("method", "township_forecast");
			selectParam.put("sqlType", "select");
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<township_forecast>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (listData != null && listData.size() > 0) {
			result = listData.get(0).getTimechar();
		}
		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		Map<String, String> selectParam1 = new HashMap<String, String>();
		RequestDataBySelect selectRequest1 = new RequestDataBySelect();
		List<String> columns1 = new ArrayList<String>();
		columns1.add("stationname");
		columns1.add("weather1");
		columns1.add("CAST(replace(cast(maxtemp AS VARCHAR),'-65535','99999') as numeric(18,1)) as maxtemp");
		columns1.add("CAST(replace(cast(mintemp AS VARCHAR),'-65535','99999') as numeric(18,1)) as mintemp");
		columns1.add("winds");
		columns1.add("windd");
		columns1.add("CAST(replace(cast(humid AS VARCHAR),'-65535','99999') as numeric(18,1)) as humid");
		columns1.add("longitude");
		columns1.add("latitude");
		selectRequest1.setColumns(columns1.toArray(new String[columns1.size()]));
		// 拼接where字段
		StringBuilder strWhere1 = new StringBuilder();
		int whereCount1 = 0;
		if (dateTime != null) {
			strWhere1.append(String.format(" and datechar='%s'", dateTime));
		}
		strWhere1.append(String.format(" and timechar='%s'", result));
		strWhere1.append(String.format(" and ntimes<='%s'", "24"));
		strWhere1.append(String.format(" and stationid like '%s%%'", DbConfig.STATIONID));
		if (whereCount1 == 0 && !StringUtil.IsNullOrEmpty(strWhere1.toString())) {
			strWhere1 = strWhere1.replace(0, 4, "");
		}
		selectRequest1.setParam(strWhere1.toString());
		try {
			selectParam1.put("method", "township_forecast");
			selectParam1.put("sqlType", "select");
			selectParam1.put("param", JsonUtil.object2Json(selectRequest1));
			String message1 = pc.processRequest(selectParam1);
			resultList = JsonUtil.jsonStr2TypeReference(message1, new TypeReference<List<Map<String, Object>>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
			System.err.println(e.getStackTrace());
		}
		return resultList;
		
	}
	@Override
	public List<Map<String, Object>> getCityFcst(boolean b, String dateTime) {
		List<township_forecast> listData = new ArrayList<township_forecast>();
		String result = "";
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("max(timeChar) as timeChar");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		if (dateTime != null) {
			strWhere.append(String.format(" and datechar='%s'", dateTime));
		}
		strWhere.append(String.format(" and ntimes='%s'", "24"));
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		
		try {
			selectParam.put("method", "township_forecast");
			selectParam.put("sqlType", "select");
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<township_forecast>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (listData != null && listData.size() > 0) {
			result = listData.get(0).getTimechar();
		}
		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		Map<String, String> selectParam1 = new HashMap<String, String>();
		RequestDataBySelect selectRequest1 = new RequestDataBySelect();
		List<String> columns1 = new ArrayList<String>();
		columns1.add("stationname");
		columns1.add("weather1");
		columns1.add("CAST(replace(cast(maxtemp AS VARCHAR),'-65535','99999') as numeric(18,1)) as maxtemp");
		columns1.add("CAST(replace(cast(mintemp AS VARCHAR),'-65535','99999') as numeric(18,1)) as mintemp");
		columns1.add("winds");
		columns1.add("windd");
		columns1.add("CAST(replace(cast(humid AS VARCHAR),'-65535','99999') as numeric(18,1)) as humid");
		columns1.add("longitude");
		columns1.add("latitude");
		selectRequest1.setColumns(columns1.toArray(new String[columns1.size()]));
		// 拼接where字段
		StringBuilder strWhere1 = new StringBuilder();
		int whereCount1 = 0;
		if (dateTime != null) {
			strWhere1.append(String.format(" and datechar='%s'", dateTime));
		}
		strWhere1.append(String.format(" and timechar='%s'", result));
		strWhere1.append(String.format(" and ntimes<='%s'", "24"));
		strWhere1.append(String.format(" and stationid like '%s%%'", DbConfig.STATIONID));
		if (whereCount1 == 0 && !StringUtil.IsNullOrEmpty(strWhere1.toString())) {
			strWhere1 = strWhere1.replace(0, 4, "");
		}
		selectRequest1.setParam(strWhere1.toString());
		try {
			selectParam1.put("method", "township_forecast");
			selectParam1.put("sqlType", "select");
			selectParam1.put("param", JsonUtil.object2Json(selectRequest1));
			String message1 = pc.processRequest(selectParam1);
			resultList = JsonUtil.jsonStr2TypeReference(message1, new TypeReference<List<Map<String, Object>>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
			System.err.println(e.getStackTrace());
		}
		return resultList;
	}

	public Object getTodayForecast() {
// 获取当前的日期和时间
		LocalDateTime currentDateTime = LocalDateTime.now();

		// 获取当前日期部分
		LocalDateTime newDateTime = currentDateTime.toLocalDate().atTime(LocalTime.of(8, 0));

		// 格式化输出为"yyyy-MM-dd HH:mm:ss"
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		String formattedDateTime = newDateTime.format(formatter);
		String timechar = "08";
		String ntimes = "24";
		List<today_forecast> listData = new ArrayList<today_forecast>();
		String result = "";
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("weather1");
		columns.add("maxtemp");
		columns.add("humid");
		columns.add("cloud");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		StringBuilder strwhere = new StringBuilder();
		strwhere.append(" stationid = '").append(DbConfig.STATIONID).append("'");
		strwhere.append(" and datechar = '").append(formattedDateTime).append("'");
		strwhere.append(" and timechar = '").append(timechar).append("'");
		strwhere.append(" and ntimes = '").append(ntimes).append("'");
		selectRequest.setParam(strwhere.toString());
		try {
			selectParam.put("method", "township_forecast");
			selectParam.put("sqlType", "select");
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<today_forecast>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		return listData;
		}


	public List<all_today_forecast> getAllTodayForecast() {
		// 获取当前的日期和时间
		LocalDateTime currentDateTime = LocalDateTime.now();

		// 获取当前日期部分
		LocalDateTime newDateTime = currentDateTime.toLocalDate().atTime(LocalTime.of(8, 0));

		// 格式化输出为"yyyy-MM-dd HH:mm:ss"
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		String formattedDateTime = newDateTime.format(formatter);
		String timechar = "08";
		String ntimes = "24";
		List<all_today_forecast> listData = new ArrayList<all_today_forecast>();
		String result = "";
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("stationid");
		columns.add("areaname");
		columns.add("maxtemp");
		columns.add("humid");
		columns.add("winds");
		columns.add("windd");
		columns.add("rain");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		StringBuilder strwhere = new StringBuilder();
		strwhere.append(" city like '%").append(DbConfig.STATIONNAME).append("%'");
		strwhere.append(" and datechar = '").append(formattedDateTime).append("'");
		strwhere.append(" and timechar = '").append(timechar).append("'");
		strwhere.append(" and ntimes = '").append(ntimes).append("'");
		selectRequest.setParam(strwhere.toString());
		try {
			selectParam.put("method", "township_forecast");
			selectParam.put("sqlType", "select");
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<all_today_forecast>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		return listData;

	}
}
